import React, {
    Component
} from 'react';
import {
    StyleSheet,
    Text,
    View,
    Dimensions,
    ImageBackground,
    AppState,
    Image,
    ToastAndroid
} from 'react-native';
import Orientation from 'react-native-orientation';
import MyANswearForType1 from './MyANswearForType1'
import MyANswearForType2 from './MyANswearForType2'
import MyANswearForType3 from './MyANswearForType3'
import MyANswearForType4 from './MyANswearForType4'

import Util from './utils'
export default class AnswearScreen extends Component {
    constructor(props) {
        super(props);
        this.state = {
            qainfo:{},
            userCode:this.props.navigation.state.params.userCode,
            userName:this.props.navigation.state.params.userName,
            ip:this.props.navigation.state.params.ip,
            intervalIndex:""
        };

    }
    static navigationOptions = {
        header: null,
        gesturesEnabled: false,
    };
    componentDidMount() {
        Orientation.lockToLandscape();
        Orientation.lockToLandscape();

        Orientation.lockToLandscape();
        this.getData()
        AppState.addEventListener('change', this.setIntervalData());
    }
    setIntervalData(){
        console.log("----------------------setIntervalData--------------------")
       let interval= setInterval(()=>this.getData()
        ,1000)
        this.setState({
            intervalIndex:interval
        })
    }
    componentWillUnmount(){
        clearInterval(this.state.intervalIndex);
    }
    componentWillMount(){
        Orientation.lockToLandscape();
        Orientation.lockToLandscape();
        Orientation.lockToLandscape();
    }
    getData(){
        const {navigate} = this.props.navigation

        let url = `http://${this.state.ip}:10081/api/getQuestionForApp?userCode=${this.state.userCode}`//人力
        let that =this
        Util.get(url, (result) => {
            if(result.status==301){
                //ToastAndroid.show('您的账号已在其他地方登录,不可以重复使用', ToastAndroid.LONG);
                //navigate("LoginScreen")
                // that.setState({
                //     qainfo:[]
                // })
            }else{
                that.setState({
                    qainfo:result
                })
            }
        },(error)=>{

        })
    }
    setAnswearQuest(){
        if(this.state.qainfo.type){
            if(this.state.qainfo.type=='多选题'){
                return(
                    <MyANswearForType1 navigation = {this.props.navigation
                    } qainfo={this.state.qainfo} userCode={this.state.userCode} userName={this.state.userName} ip={this.state.ip}></MyANswearForType1>
                )
             }
            else if(this.state.qainfo.type=='单选题' ){
                return(
                    <MyANswearForType2 navigation = {this.props.navigation
                    } qainfo={this.state.qainfo} userCode={this.state.userCode} userName={this.state.userName} ip={this.state.ip}></MyANswearForType2>
                )
            }
            else if(this.state.qainfo.type=='投票题'){
                return(
                    <MyANswearForType4 navigation = {this.props.navigation
                    } qainfo={this.state.qainfo} userCode={this.state.userCode} userName={this.state.userName} ip={this.state.ip}></MyANswearForType4>
                )
            }else{
                return(
                    <MyANswearForType3 navigation = {this.props.navigation
                    } qainfo={this.state.qainfo} userCode={this.state.userCode} userName={this.state.userName} ip={this.state.ip}></MyANswearForType3>
                ) 
            }
        }else{
            return(
                <View style={{height:Dimensions.get('window').height,justifyContent:'center',alignItems:'center'}}>
                    <Text style={{fontSize:48}}>请等待主持人出题</Text> 
                </View>
            )
        }
    }
    render() {
        return (
           <View style={[styles.main]}>
                <ImageBackground source={require('./image/mainback.png')} style={{width:Dimensions.get('window').width,height:Dimensions.get('window').height-25}} resizeMode='stretch'>
                    <Image style={styles.topicon} source={require('./image/logo.png')}/>
                    {this.setAnswearQuest()}
                </ImageBackground>
            </View>
        )
    }
}
const styles = StyleSheet.create({
    main:{
        flex: 1,
        backgroundColor:'#ffffff',
    },
    fontFormart:{
        fontSize: 18,
    },
    topicon:{
        height:250,
        width:250,
        position: 'absolute',
        right:-10,
        top:-30,
        zIndex:999
    },
    bottomicon:{
        height:15,
    },
})